/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 驿站站点管理-驿站敏感词管理
 */
import React, { useRef } from 'react';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import type { TableListItem } from '@/components/_pages/yz/manage/index.d';
import { delMinGanCi, getMinGanCiList } from '@/services/yz/sensitive';
import { Button, message, Popconfirm, Typography } from 'antd';
import type { StringObjType } from '../../AmountApply/index.d';
import SensitiveWordModal from './SensitiveWordModal';
const { Text } = Typography;

const SensitiveWordTable = ({ activeKey }: { activeKey: string }) => {
  const actionRef = useRef<ActionType>();

  const onDelete = async (id: string) => {
    const { code, msg } = await delMinGanCi({ id });
    if (code == 0) {
      message.info('删除成功');
      actionRef.current?.reload();
    } else {
      message.error(msg);
    }
  };

  const columns: ProColumns<TableListItem>[] = [
    {
      title: '关键词',
      dataIndex: 'word',
      align: 'center',
    },
    {
      title: '创建人',
      dataIndex: 'create_user',
      align: 'center',
    },
    {
      title: '创建时间',
      dataIndex: 'created_at',
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'operate',
      align: 'center',
      render: (_, record: any) => {
        const { id, word = '' } = record || {};
        return (
          <Popconfirm
            onConfirm={onDelete.bind(null, id)}
            title={
              <>
                <Text>确定删除关键词</Text>
                <Text type='danger'> {word} </Text>
                <Text>吗？</Text>
              </>
            }
          >
            <Button size='small' type='primary'>
              删除
            </Button>
          </Popconfirm>
        );
      },
    },
  ];

  const requestTableData = async (values: StringObjType) => {
    const { activeKey: key, current, pageSize } = values;
    const { code, data, msg } = await getMinGanCiList({
      page: current,
      limit: pageSize,
      type: key,
    });
    if (code == 0) {
      return { data: data.list, total: data.count };
    } else {
      message.error(msg);
      return { data: [], total: 0 };
    }
  };

  console.info('actionRef', actionRef.current);

  const onReset = () => {
    if (actionRef.current?.reset) {
      actionRef.current?.reset();
    }
  };

  return (
    <DragTable>
      <ProTable<TableListItem>
        actionRef={actionRef}
        params={{ activeKey }}
        rowKey='id'
        request={requestTableData}
        columns={columns}
        options={false}
        pagination={{
          pageSize: 20,
          showQuickJumper: false,
          showSizeChanger: false,
        }}
        headerTitle={<SensitiveWordModal activeKey={activeKey} reload={onReset} />}
        scroll={{ x: 1000 }}
        search={false}
      />
    </DragTable>
  );
};

export default SensitiveWordTable;
